import 'package:backend.core/core/domain/entities/app_store/app_store_purchased_subscription_expiration_reason.dart';
import 'package:backend.core/core/domain/entities/app_store/app_store_purchased_subscription_info.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_purchased_subscription_renewal_info.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_expiration_reason_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_price_increase_status_from_api_one.dart';

abstract class UpdateAppStorePurchasedSubscriptionInfoFromApiOne {
  AppStorePurchasedSubscriptionInfo call({
    required AppStorePurchasedSubscriptionInfo purchasedSubscriptionInfo,
    required AppStoreApiPurchasedSubscriptionRenewalInfo updatedApiRenewalInfo,
  });
}

class UpdateAppStorePurchasedSubscriptionInfoFromApiOneImpl
    implements UpdateAppStorePurchasedSubscriptionInfoFromApiOne {
  const UpdateAppStorePurchasedSubscriptionInfoFromApiOneImpl({
    required CreateAppStorePurchasedSubscriptionExpirationReasonFromApiOne
        createAppStorePurchasedSubscriptionExpirationReasonFromApiOne,
    required CreateAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne
        createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne,
  })  : _createAppStorePurchasedSubscriptionExpirationReasonFromApiOne =
            createAppStorePurchasedSubscriptionExpirationReasonFromApiOne,
        _createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne =
            createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne;

  final CreateAppStorePurchasedSubscriptionExpirationReasonFromApiOne
      _createAppStorePurchasedSubscriptionExpirationReasonFromApiOne;
  final CreateAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne
      _createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne;

  @override
  AppStorePurchasedSubscriptionInfo call({
    required AppStorePurchasedSubscriptionInfo purchasedSubscriptionInfo,
    required AppStoreApiPurchasedSubscriptionRenewalInfo updatedApiRenewalInfo,
  }) {
    AppStorePurchasedSubscriptionExpirationReason? updatedExpirationReason;

    if (updatedApiRenewalInfo.expirationReason != null) {
      updatedExpirationReason = _createAppStorePurchasedSubscriptionExpirationReasonFromApiOne(
        apiExpirationReason: updatedApiRenewalInfo.expirationReason!,
      );
    }

    final updatedPriceIncreaseStatus =
        _createAppStorePurchasedSubscriptionPriceIncreaseStatusFromApiOne(
      apiPriceIncreaseStatus: updatedApiRenewalInfo.priceIncreaseStatus,
    );

    final updatedPurchasedSubscriptionInfo = purchasedSubscriptionInfo.copyWith(
      purchaseItemSku: () => updatedApiRenewalInfo.purchaseItemSku,
      originalTransactionId: () => updatedApiRenewalInfo.originalTransactionId,
      autoRenewalEnabled: () => updatedApiRenewalInfo.autoRenewalEnabled,
      isInBillingRetryPeriod: () => updatedApiRenewalInfo.isInBillingRetryPeriod,
      priceIncreaseStatus: () => updatedPriceIncreaseStatus,
      expirationReason: () => updatedExpirationReason,
      gracePeriodExpirationTime: () => updatedApiRenewalInfo.gracePeriodExpirationTime,
    );

    return updatedPurchasedSubscriptionInfo;
  }
}
